<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/7  17:10
 */
namespace app\api\logic;
use think\Cache;
use think\Request;
use app\api\validate\Member as MemberValidate;

class Member extends \app\api\BaseModel
{
    //检测参数是否已传
    protected $ParameterDetection  = [
        'buyerCheck'=>['name', 'password', 'id_card', 'mobile'],
        'headimagEdit'=>['headimage'],
        'hxInfo'=>['hx_username'],
    ];
    /*
    买手申请
    */
    public function buyerCheck()
    {
        (new MemberValidate())->checkBuyerParam();
        $data = [
            'name'=>trim($this->requestData['name']),
            'password'=>trim($this->requestData['password']),
            'id_card'=>trim($this->requestData['id_card']),
            'mobile'=>trim($this->requestData['mobile']),
            'is_agree'=>trim($this->requestData['is_agree']),
        ];

        if($data['is_agree']!=1)return ['message'=>'是否同意协议！'];

        $code = trim($this->requestData['code']);
        if (base64_encode($code) != Cache::get($data['mobile'])) {
           return array('message'=>'验证码错误');
        }
        $uid = $this->user['id'];
        if(empty($uid))return ['message'=>'不是会员没权限注册买手！'];
        $db_buyer = db('member_buyer_check');
        $buyer = $db_buyer->where('user_id='.$uid)->find();
        if(!empty($buyer))return ['message'=>'你已注册了买手！'];

        $where['id_card'] = $data['id_card'];
        $buyer = $db_buyer->where($where)->find();
        if(!empty($buyer))return array('message'=>'该身份证已被注册！');

        $where['mobile'] = $data['mobile'];
        $buyer = $db_buyer->where($where)->find();
        if(!empty($buyer))return array('message'=>'该电话号码已被注册！');

        $data['password'] = md5($data['password']);
        $data['user_id'] = $uid;
        $data['create_time'] = time();

        $res = $db_buyer->insert($data);
        if(!$res)return array('message'=>'注册失败！');
    }

    //修改头像
    public function headimagEdit()
    {
        $uid = $this->user['id'];
        $file = request()->file('headimage');
        if ($file) {
            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/static/uploads');
            if ($info) {
                $data['headimage'] = $info->getSaveName();
            }else {
                return array('message'=>$file->getError());
            }
        }
        $member = db('member')->where('id='.$uid)->find();
        if(!empty($member['headimage'])){
            if (is_file(ROOT_PATH . 'public/static/uploads/' . $member['headimage'])) {
                unlink(ROOT_PATH . 'public/static/uploads/' . $member['headimage']);
            }
        }
        $res = db('member')->where('id='.$uid)->update($data);
        if(!$res) return ['message'=>'修改失败'];
    }

    //用户详情
    public function memberInfo(){
        $uid = $this->user['id'];
        $member_info = db('member')->where('id='.$uid)->find();
        $member_buyer_check = db('member_buyer_check')->where(['user_id'=>$uid])->value('status');
        $member_info['is_buy_status'] = $member_buyer_check;
        return $member_info;
    }

    //环信用户昵称，头像
    public function hxInfo()
    {
        $hxuser = $this->requestData['hx_username'];
        $hx_result = db('member')->where(['hx_username'=>$hxuser])->field('nickname,headimage,hx_username')->find();
        if(!$hx_result){
            return ['message'=>'参数不存在'];
        }
        return $hx_result;
    }
}